/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reportcenter.datasource.ui;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import org.springframework.binding.value.ValueModel;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.application.event.LifecycleApplicationEvent;
import org.springframework.richclient.application.support.AbstractView;
import org.springframework.richclient.command.ActionCommand;
import org.springframework.richclient.command.ActionCommandExecutor;
import org.springframework.richclient.command.CommandGroup;
import org.springframework.richclient.command.GuardedActionCommandExecutor;
import org.springframework.richclient.command.support.AbstractActionCommandExecutor;
import org.springframework.richclient.command.support.GlobalCommandIds;
import org.springframework.richclient.dialog.ConfirmationDialog;
import org.springframework.richclient.list.ListSelectionValueModelAdapter;
import org.springframework.richclient.list.ListSingleSelectionGuard;
import reportcenter.datasource.business.DataSourceDao;
import reportcenter.datasource.dominio.DataSource;

/**
 *
 * @author renan
 */

public class DataSourceView extends AbstractView implements ApplicationListener{
    
    private DataSourceDao dataSourceDao;
    private DataSourceTable dataSourceTable;

    public DataSourceDao getDataSourceDao() {
        return dataSourceDao;
    }

    public void setDataSourceDao(DataSourceDao dataSourceDao) {
        this.dataSourceDao = dataSourceDao;
    }

    public DataSourceTable getDataSourceTable() {
        return dataSourceTable;
    }

    public void setDataSourceTable(DataSourceTable dataSourceTable) {
        this.dataSourceTable = dataSourceTable;
    }
    

    private JTextField filterField;
    @Override
    protected JComponent createControl() {
        
        JPanel filterPanel = new JPanel(new BorderLayout());
        JLabel filterLabel = getComponentFactory().createLabel("dataSourceFilter.label");
        filterPanel.add(filterLabel, BorderLayout.WEST);

        String tip = getMessage("dataSourceFilter.caption");
        filterField = getComponentFactory().createTextField();
        filterField.setToolTipText(tip);
        filterPanel.add(filterField, BorderLayout.CENTER);
        filterPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        //instancia tabela de relatórios
        dataSourceTable = new DataSourceTableFactory().createDataSourceTable();

        JPanel view = new JPanel(new BorderLayout());
        JScrollPane sp = getComponentFactory().createScrollPane(dataSourceTable.getControl());
        view.add(filterPanel, BorderLayout.NORTH);
        view.add(sp, BorderLayout.CENTER);
        
        return view;
    }
    
    private class DataSourceTableFactory {

        public DataSourceTable createDataSourceTable() {
            DataSourceTable dataSourceTable = new DataSourceTable(dataSourceDao);

            dataSourceTable.setDoubleClickHandler(propExecutor);

            //Evento de filtro
            EventList baseList = dataSourceTable.getBaseEventList();
            TextFilterator filterator = GlazedLists.textFilterator(new String[]{"name", "driver", "usu"});
            FilterList filterList = new FilterList(baseList, new TextComponentMatcherEditor(filterField, filterator));
            dataSourceTable.setFinalEventList(filterList);

            //Configuração do menu de popup
            CommandGroup popup = new CommandGroup();
            popup.add((ActionCommand) getWindowCommandManager().getCommand("propertiesCommand", ActionCommand.class));
            popup.add((ActionCommand) getWindowCommandManager().getCommand("deleteCommand", ActionCommand.class));
            dataSourceTable.setPopupCommandGroup(popup);
            dataSourceTable.setStatusBar(getStatusBar());


            ValueModel selectionHolder = new ListSelectionValueModelAdapter(dataSourceTable.getSelectionModel());
            new ListSingleSelectionGuard(selectionHolder, propExecutor);
            new ListSingleSelectionGuard(selectionHolder, delExecutor);

            return dataSourceTable;
        }
    }
    
    private GuardedActionCommandExecutor propExecutor = new PropExecutor();
    private class PropExecutor extends AbstractActionCommandExecutor {

        public void execute() {
            new DataSourcePropertiesDialog(dataSourceTable.getSelectedDataSource(), getDataSourceDao()).showDialog();
        }
    }
    
    private GuardedActionCommandExecutor delExecutor = new DelExecutor();
    private class DelExecutor extends AbstractActionCommandExecutor {

        public void execute() {
            // We know exactly one contact will be selected at this time because
            // of the guards put in place in prepareTable.
            final DataSource dataSource = dataSourceTable.getSelectedDataSource();
            // Query the user to be sure they want to do this
            String title = getMessage("DataSourceView.confirmDelete.title");
            String message = getMessage("DataSourceView.confirmDelete.message");
            ConfirmationDialog dlg = new ConfirmationDialog(title, message) {

                protected void onConfirm() {
                    // Delete the object from the persistent store.
                    getDataSourceDao().delete(dataSource);
                    // And notify the rest of the application of the change
                    getApplicationContext().publishEvent(
                            new LifecycleApplicationEvent(LifecycleApplicationEvent.DELETED, dataSource));
                }
            };
            dlg.showDialog();
        }
    }
    
    private ActionCommandExecutor newExecutor = new NewExecutor();
    private class NewExecutor implements ActionCommandExecutor {

        public void execute() {
            new DataSourcePropertiesDialog(getDataSourceDao()).showDialog();
        }
    }
    
    private ActionCommandExecutor refreshExecutor = new RefreshExecutor();

    private class RefreshExecutor implements ActionCommandExecutor {

        public void execute() {
            dataSourceTable.refreshTable();
        }
    }
    
    protected void registerLocalCommandExecutors(PageComponentContext context) {
        context.register("newCommand", newExecutor);
        context.register(GlobalCommandIds.PROPERTIES, propExecutor);
        context.register(GlobalCommandIds.DELETE, delExecutor);
        context.register("refreshCommand", refreshExecutor);
    }
    

    public void onApplicationEvent(ApplicationEvent arg0) {
        dataSourceTable.onApplicationEvent(arg0);
    }
}
